package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/Document", "/document", "/DocumentServlet",
		"/documentServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class DocumentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public DocumentServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<DocumentModel> checkInsertRight()) {

				// TODO : MORE CODE HERE

				getServletContext().getRequestDispatcher("/documentNew.jsp")
						.forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

			DocumentModel query = new DocumentModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<DocumentModel, DocumentModelExt> dal = new GenericDal<DocumentModel, DocumentModelExt>(
					new DocumentModel());

			DocumentModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// TODO : MORE CODE HERE

				request.setAttribute("document", obj);
				getServletContext().getRequestDispatcher("/documentEdit.jsp")
						.forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("view")) {

			DocumentModel query = new DocumentModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<DocumentModel, DocumentModelExt> dal = new GenericDal<DocumentModel, DocumentModelExt>(
					new DocumentModel());

			DocumentModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {
				// TODO : LOAD MASTER DATA HERE
				// It's specific code that cannot be generated

				request.setAttribute("document", obj);
				getServletContext().getRequestDispatcher("/documentView.jsp")
						.forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			DocumentModel obj = new DocumentModel();

			// TODO SET NAME here

			GenericDal<DocumentModel, DocumentModelExt> dal = new GenericDal<DocumentModel, DocumentModelExt>(
					new DocumentModel());

			int res = 0;
			try {
				res = dal.insert(obj);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {

			DocumentModel obj = new DocumentModel();

			String name = request.getParameter("name");
			obj.setName(name);

			String code = request.getParameter("code");
			obj.setCode(code);

			String description = request.getParameter("description");
			obj.setDescription(description);

			String is_template = request.getParameter("is_template");
			obj.setIsTemplate(is_template.equalsIgnoreCase("true")
					|| is_template.equalsIgnoreCase("1"));

			String object_type = request.getParameter("object_type");
			obj.setObjectType(object_type);

			String object_id = request.getParameter("object_id");
			if (object_id != null && !object_id.isEmpty()) {
				obj.setObjectId(Integer.parseInt(object_id));
			}

			String need_approval = request.getParameter("need_approval");
			obj.setNeedApproval(need_approval.equalsIgnoreCase("true")
					|| need_approval.equalsIgnoreCase("1"));

			String status = request.getParameter("status");
			if (status != null && !status.isEmpty()) {
				obj.setStatus(Integer.parseInt(status));
			}

			String is_draft = request.getParameter("is_draft");
			obj.setIsDraft(is_draft.equalsIgnoreCase("true")
					|| is_draft.equalsIgnoreCase("1"));

			String notification = request.getParameter("notification");
			if (notification != null && !notification.isEmpty()) {
				obj.setNotification(Integer.parseInt(notification));
			}

			obj.setCreator(SessionUtil.getCurrentUserID());

			if (CommonBll.checkInputs(obj)) {
				GenericDal<DocumentModel, DocumentModelExt> dal = new GenericDal<DocumentModel, DocumentModelExt>(
						new DocumentModel());

				int res = 0;
				try {
					res = dal.insert(obj);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res > 0) {
					// TODO : success, redirect to somewhere
				} else {
					// TODO : processing error here
				}
			} else {

				// invalid input

				request.setAttribute("document", obj);
				getServletContext().getRequestDispatcher("/documentInput.jsp")
						.forward(request, response);

			}
		} else if (mode.equalsIgnoreCase("update")) {

			DocumentModel query = new DocumentModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<DocumentModel, DocumentModelExt> dal = new GenericDal<DocumentModel, DocumentModelExt>(
					new DocumentModel());

			DocumentModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				String name = request.getParameter("name");
				obj.setName(name);

				String code = request.getParameter("code");
				obj.setCode(code);

				String description = request.getParameter("description");
				obj.setDescription(description);

				String is_template = request.getParameter("is_template");
				obj.setIsTemplate(is_template.equalsIgnoreCase("true")
						|| is_template.equalsIgnoreCase("1"));

				String object_type = request.getParameter("object_type");
				obj.setObjectType(object_type);

				String object_id = request.getParameter("object_id");
				obj.setObjectId(Integer.parseInt(object_id));

				String need_approval = request.getParameter("need_approval");
				obj.setNeedApproval(need_approval.equalsIgnoreCase("true")
						|| need_approval.equalsIgnoreCase("1"));

				String status = request.getParameter("status");
				obj.setStatus(Integer.parseInt(status));

				String is_draft = request.getParameter("is_draft");
				obj.setIsDraft(is_draft.equalsIgnoreCase("true")
						|| is_draft.equalsIgnoreCase("1"));

				String notification = request.getParameter("notification");
				obj.setNotification(Integer.parseInt(notification));

				if (CommonBll.checkInputs(obj)) {

					int res = Integer.MIN_VALUE;
					try {
						res = dal.update(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res == 0) {
						// TODO success
					} else {
						// TODO error
					}
				} else {
					request.setAttribute("document", obj);
					getServletContext().getRequestDispatcher(
							"/documentEdit.jsp").forward(request, response);
				}
			} else {
				// TODO data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			DocumentModel query = new DocumentModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<DocumentModel, DocumentModelExt> dal = new GenericDal<DocumentModel, DocumentModelExt>(
					new DocumentModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
			} else {
				// TODO fail
			}
		}
	}

}